﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms.Design;
using System.Reflection;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Design;
using UIDesigner.Designers;
using UIDesigner.DesignContainers.Bases;
using UIDesigner.Design;

namespace UIDesigner.SurfaceProperties
{
    public class SurfacePropertiesTab : PropertyTab
    {
        private AFDesignerHost m_objHost;
        private PropertyDescriptorCollection m_rgPropertyCollection;

        public SurfacePropertiesTab()
        {
        }

        public void Refresh()
        {
            m_rgPropertyCollection = null;
        }

        private PropertyDescriptorCollection SurfacePropertyCollection
        {
            get
            {
                if (m_rgPropertyCollection == null)
                {
                    PropertyDescriptor[] rgProperties;
                    Attribute[] rgAttr = new Attribute[1];
                    int x = 0;
                    List<SurfaceObjectDescriptor> rgObjectDescriptors = new List<SurfaceObjectDescriptor>();

                    rgAttr[0] = new TypeConverterAttribute(typeof(ExpandableObjectConverter));

                    foreach (IComponent objComponent in ((IContainer)m_objHost).Components)
                    {
                        IBaseObjectControl objControl = objComponent as IBaseObjectControl;

                        if (objControl != null)
                        {
                            SurfaceObjectDescriptor objDescriptor = new SurfaceObjectDescriptor(m_objHost, objControl, rgAttr);
                            rgObjectDescriptors.Add(objDescriptor);
                        }
                    }

                    rgProperties = new PropertyDescriptor[rgObjectDescriptors.Count];

                    foreach (SurfaceObjectDescriptor objDescriptor in rgObjectDescriptors)
                    {
                        rgProperties[x] = objDescriptor;
                        x++;
                    }

                    m_rgPropertyCollection = new PropertyDescriptorCollection(rgProperties);
                }

                return m_rgPropertyCollection;
            }
        }

        public override PropertyDescriptorCollection GetProperties(object component, Attribute[] attributes)
        {
            if (component is Control)
            {
                m_objHost = (AFDesignerHost)((Control)component).Site.Container;

                return this.SurfacePropertyCollection;
            }
            else if (component is IPropertyCollectionProvider)
            {
                IPropertyCollectionProvider objProvider = (IPropertyCollectionProvider)component;

                return objProvider.PropertyCollection; 
            }

            return null;
        }

        public override Bitmap Bitmap
        {
            get
            {
                System.Reflection.Assembly objAssm;
                objAssm = System.Reflection.Assembly.GetExecutingAssembly();
                System.IO.Stream objFile = objAssm.GetManifestResourceStream("UIDesigner.Media.Surface.bmp");
                
                return (Bitmap) Image.FromStream(objFile);
            }
        }

        public override string TabName
        {
            get 
            { 
                return "Surface Properties"; 
            }
        }
    }
}
